package com.jerry.usbserial.util

import android.os.Build
import io.reactivex.Observable
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST

object RemoteRepo {
    private const val baseUrl = "http://new.zniot.com:9002"

    private val httpBuilder = Retrofit.Builder()
        .baseUrl(baseUrl)
        .client(
            OkHttpClient.Builder().apply {
                addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
            }.build()
        )
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    fun getApi(): Api = httpBuilder.create(Api::class.java)
}


interface Api {
    @FormUrlEncoded
    @POST("page?")
    fun commit(
        @Field("sn") sn: String,
        @Field("storeid") storeid: String,
        @Field("storename") storename: String,
        @Field("user") user: String,
        @Field("tel") tel: String,
        @Field("address") address: String,
        @Field("date") date:String = "",
        @Field("log") log:String = "",
        @Field("cave") cave: String = "uhc",
        @Field("func") func: String = "1",
        @Field("token") token: String = "token",
        @Field("mobile") mobile: String = Build.BRAND,
        @Field("version") version: String = Build.MODEL,
        @Field("end") end: String = "end"
    ): Observable<CommitResult>

    @FormUrlEncoded
    @POST("page?")
    fun menuItemList(
        @Field("cave") cave: String = "tablelist",
        @Field("func") func: String = "func",
        @Field("token") token: String = "token",
        @Field("end") end: String = "end"
    ): Observable<List<MenuItem>>
}

